package br.com.autoleal.cliente.entity;

import java.io.Serializable;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class ClienteServiceBean implements Serializable {

	private static final long serialVersionUID = -8514013594036882901L;

	@PersistenceContext
	private EntityManager entityManager;

	public void incluirCliente(Cliente cliente) {
		entityManager.persist(cliente);
	}

	public void alterarCliente(Cliente cliente) {
		entityManager.merge(cliente);
	}

	public void excluirCliente(Cliente cliente) {
		cliente = entityManager.find(Cliente.class, cliente.getId());
		entityManager.remove(cliente);
	}

	@SuppressWarnings("unchecked")
	public List<Cliente> buscarTodos() {
		Query query = entityManager.createNamedQuery("Cliente.buscarTodos");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Cliente> buscarTodosNome(String apelido) {
		Query query;
		query = entityManager.createNamedQuery("Cliente.buscarTodosNome");
		query.setParameter("apelido","%"+ apelido.toUpperCase() +"%");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			System.out.println("Erro ao consultar");
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PessoaFisica> buscarCodigoCpfNome(String consulta) {
		Query query = entityManager.createNamedQuery("PessoaFisica.buscarCodigoCpfNome");
		query.setParameter("codigo", "%" + consulta + "%");
		query.setParameter("cpf", "%" + consulta + "%");
		query.setParameter("nome", "%" + consulta.toUpperCase() + "%");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PessoaJuridica> buscarCodigoCnpjNome(String consulta) {
		Query query = entityManager.createNamedQuery("PessoaJuridica.buscarCnpjRazaoNoFantasiaCodigo");
		query.setParameter("codigo", "%" + consulta + "%");
		query.setParameter("cnpj", "%" + consulta + "%");
		query.setParameter("razaoSocial", "%" + consulta.toUpperCase() + "%");
		query.setParameter("noFantasia", "%" + consulta.toUpperCase() + "%");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<PessoaFisica> buscarTodosPessoaFisica() {
		Query query = entityManager.createNamedQuery("PessoaFisica.buscarTodos");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<PessoaJuridica> buscarTodosPessoaJuridica() {
		Query query = entityManager.createNamedQuery("PessoaJuridica.buscarTodos");
		try {
			return query.getResultList();
		} catch (NoResultException e) {
			return null;
		}
	}

	public String buscarCodigoMax() {
		Query query = entityManager.createNamedQuery("Cliente.buscarCodigoMax");
		try {
			return String.valueOf(Integer.valueOf(query.getSingleResult().toString().replaceAll("0", "")) + 1);
		} catch (Exception e) {
			return "1";
		}
	}

}
